/**
 * Created by meil01 on 2019.
 */
 import jQuery  from 'jquery'

 jQuery.extend({
    isIE:function(){
        if (!!window.ActiveXObject || "ActiveXObject" in window){ 
            return true;
        }else{
            return false;
        }
    },
    isBelowIE:function(){
        if(!jQuery.isIE()) return false;
        var ua = window.navigator.userAgent;
        var args = Array.prototype.slice.apply(arguments);
        for(var i=0;i<args.length;i++){
            var vi = args[i];
            if(ua.indexOf("MSIE "+vi+".0")>-1){
                return true;
            }
        }
        return false;
    },
    waterMark:function(curUserName){
        var text = '';
        var _date = new Date();
        var day = _date.getDate() < 10 ? "0" + _date.getDate() : _date.getDate();
        var hour = _date.getHours() < 10 ? "0" + _date.getHours() : _date.getHours();
        var minute = _date.getMinutes() < 10 ? "0" + _date.getMinutes() : _date.getMinutes();
        var second = _date.getSeconds() < 10 ? "0" + _date.getSeconds() : _date.getSeconds();

        var repeatCount = 100;
        var spanAr = [];
        var data1 = curUserName;
        var data2 = _date.getFullYear() + "-" + (_date.getMonth() + 1) + "-" + day + " " + hour + ":" + minute + ":" + second;
    
        data2 = data2.split("");
    
        var str = /^\w+$/;
        var isEn = str.test(data1);
        data1 = data1.split("");
    
        jQuery.each(data1, function (index,item) {
            if (isEn) {//如果是纯英文，则需要少一点偏移
                text += '<span style="margin-top:' + ((data1.length - index) * 3 + data2.length * 3) + 'px;">' + item + '</span>';
            } else {
                text += '<span style="margin-top:' + ((data1.length - index) * 6 + data2.length * 3) + 'px;">' + item + '</span>';
            }
        });
    
        jQuery.each(data2, function (index,item) {
            text += '<span style="margin-top:' + (data2.length - index) * 3 + 'px;">' + item + '</span>';
        })
    
        for (var i = 0; i < repeatCount; i++) {
            spanAr.push("<p class='mark-text'>" + text + "</p>");
        }
        var watermarkContainerIE = '<div class="watermark-mask-ie"> <div class="mark-zz"></div>' + spanAr.join('') + '</div>';
        jQuery(watermarkContainerIE).appendTo("body");
    }
});
let mysoftForm = {
    api:{}
}
var api = mysoftForm.api;
api.messager = (function(){
    var me = {};
    me.observers = {};
    
    me.send = function(data,options,transfer){
        
        if(!data || !options) return;
        var w = options.targetWindow;
        var orgin = options.targetOrigin||"*";
        var transfer = options.transfer || null;
        if(!w) return;
        if(jQuery.isBelowIE(8,9)){
            data = JSON.stringify(data);
        }
        if(transfer) w.postMessage(data,orgin,transfer);
        else w.postMessage(data,orgin);
    };

    me.receive = function(e){
        var d = null;
        if(jQuery.isBelowIE(8,9)){
            d = JSON.parse(e.data);
        }
        
        for(var id in me.observers){
            var ob = me.observers[id];
            ob && ob.receive && ob.receive(d?d:e.data,e.orgin,e.source);
        }
    };
    if(window.addEventListener){
        window.addEventListener("message",me.receive,false);
    }else{
        window.attachEvent("onmessage",me.receive);
    }

    return {
        addObserver:function(ob,id){
            if(!id) id = "ob_" + jQuery.rndNum(5);
            me.observers[id] = ob;
        },
        removeObserver:function(id){
            delete me.observers[id];
        },
        send:me.send,
        receive:me.receive
    };
})();


api.client = (function(){
    function observer(options,adapter,transfer){
        if(!options || !adapter) throw "args : options or adapter not be seted";
        var id = "ob_"+jQuery.rndNum(5);
        var msgr = mysoftForm.api.messager;
        var me = this;
        me.init = function(){
            msgr.addObserver(this,id);
            adapter.setData = me.send;
            me.receiveHandler = adapter.getData;
        };
        me.getId = function(){
            return id;
        };
        me.send = function(data){
            if(me.filterSubject(data) && adapter.filterSend){
                var d = adapter.filterSend(data);
                msgr.send(d,options,transfer);
            }
        };
        me.receiveHandler = null;
        me.receive = function(data,orgin,source){
            if(me.filterSubject(data) && adapter.filterReceive){
                var d = adapter.filterReceive(data,orgin,source);
                me.receiveHandler && me.receiveHandler(data.subject,d);
            }
        };
        me.filterSubject = function(data){
            var subjects = adapter.getSubjects?adapter.getSubjects():null;
            if(!data || !data.subject || !subjects) return false;
            if((typeof subjects === "string" && subjects === "*")
            || (subjects instanceof Array && subjects.indexOf(data.subject) > -1)){
               return true;
            }
            return false;
        };
        
    };

    return{
        create : function(options,adapter,transfer){
            return new observer(options,adapter,transfer);
        }        
    };
})();


export {mysoftForm}